﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LLClient.SaveAndLoad;
using Microsoft.Win32;
using LLClient.ClientServiceRef;
using LLClient.ServerProxy;
using System.Collections.ObjectModel;
using System.Data;

namespace LLClient.Portal
{
    /// <summary>
    /// Interaction logic for StatsControl.xaml
    /// </summary>
    public partial class StatsControl : UserControl
    {
        private Proxy _proxy = Proxy.GetInstance();
        private bool _eventRegistered = false;
        private Client _currentClient = Proxy.GetInstance().CurrentClient;

        private List<BattleReport> _battleReport;
        private List<CptStats> _captainStats;
        private List<FractionStats> _fractionStats;
        private List<Captain> _captainList;
        private List<BattleData> battleData;

        public StatsControl()
        {
            InitializeComponent();

            if (!_eventRegistered)
            {
                if (_proxy != null)
                    _proxy.ProxyStatisticCallbackEvent += ProxyStatisticCallBackEvent;
                _eventRegistered = true;
            }
            statComboBox.IsEnabled = false;
            battleData = new List<BattleData>();
        }

        public void ProxyStatisticCallBackEvent(object sender, ProxyStatisticCallbackArgs e)
        {
            Dispatcher.BeginInvoke(new Action(() =>
            {
                if (e != null)
                {
                    if (e.CallbackType == CallbackType.ReceiveStatistic)
                    {
                        _battleReport = e.BattleReport;
                        _captainStats = e.CaptainStats;
                        _fractionStats = e.FractionStats;
                        _captainList = e.CaptainList;

                        foreach (BattleReport b in _battleReport)
                        {
                            BattleData bd = new BattleData();
                            int index = -1;
                            for(int i = 0; i < _captainList.Count; i++)
                                if (_captainList[i].Id == b.Cpt1_id)
                                {
                                    index = i;
                                    break;
                                }
                            bd.Player1 = _captainList[index].Name;

                            for (int i = 0; i < _captainList.Count; i++)
                                if (_captainList[i].Id == b.Cpt2_id)
                                {
                                    index = i;
                                    break;
                                }
                            bd.Player2 = _captainList[index].Name;

                            for (int i = 0; i < _captainList.Count; i++)
                                if (_captainList[i].Id == b.Winner_id)
                                {
                                    index = i;
                                    break;
                                }
                            bd.Winner = _captainList[index].Name;

                            battleData.Add(bd);
                        }

                        statComboBox.IsEnabled = true;
                    }
                }
            }));
        }

        public void windowClose(object sender, EventArgs e)
        {
            if (_proxy != null)
                _proxy.ProxyStatisticCallbackEvent -= ProxyStatisticCallBackEvent;
            _eventRegistered = false;
        }

        private void statComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            statDataGrid.ItemsSource = null;
            statDataGrid.Columns.Clear();
            statDataGrid.Items.Clear();
            
            if (statComboBox.SelectedIndex == 0) // battle
            {
                DataGridTextColumn textColumn1 = new DataGridTextColumn();
                textColumn1.Header = "Player 1";
                textColumn1.Binding = new Binding("Player1");
                statDataGrid.Columns.Add(textColumn1);

                DataGridTextColumn textColumn2 = new DataGridTextColumn();
                textColumn2.Header = "Player 2";
                textColumn2.Binding = new Binding("Player2");
                statDataGrid.Columns.Add(textColumn2);

                DataGridTextColumn textColumn3 = new DataGridTextColumn();
                textColumn3.Header = "Winner";
                textColumn3.Binding = new Binding("Winner");
                statDataGrid.Columns.Add(textColumn3);

                statDataGrid.ItemsSource = battleData;
                statDataGrid.Columns.RemoveAt(5);
                statDataGrid.Columns.RemoveAt(4);
                statDataGrid.Columns.RemoveAt(3);
                statDataGrid.InvalidateVisual();
            }
            else if (statComboBox.SelectedIndex == 1) // captain
            {
                DataGridTextColumn textColumn1 = new DataGridTextColumn();
                textColumn1.Header = "Captain Name";
                textColumn1.Binding = new Binding("Captain_Name");
                statDataGrid.Columns.Add(textColumn1);

                DataGridTextColumn textColumn2 = new DataGridTextColumn();
                textColumn2.Header = "Battle Win";
                textColumn2.Binding = new Binding("Battles_won");
                statDataGrid.Columns.Add(textColumn2);

                DataGridTextColumn textColumn3 = new DataGridTextColumn();
                textColumn3.Header = "Battle Lost";
                textColumn3.Binding = new Binding("Battles_lost");
                statDataGrid.Columns.Add(textColumn3);
                
                statDataGrid.ItemsSource = _captainStats;
                statDataGrid.Columns.RemoveAt(6);
                statDataGrid.Columns.RemoveAt(5);
                statDataGrid.Columns.RemoveAt(4);
                statDataGrid.Columns.RemoveAt(3);
                statDataGrid.InvalidateVisual();
            }
            else // fraction
            {
                DataGridTextColumn textColumn1 = new DataGridTextColumn();
                textColumn1.Header = "Fraction Name";
                textColumn1.Binding = new Binding("Fraction_name");
                statDataGrid.Columns.Add(textColumn1);
                
                DataGridTextColumn textColumn2 = new DataGridTextColumn();
                textColumn2.Header = "Battle Win";
                textColumn2.Binding = new Binding("Battles_won");
                statDataGrid.Columns.Add(textColumn2);

                DataGridTextColumn textColumn3 = new DataGridTextColumn();
                textColumn3.Header = "Battle Lost";
                textColumn3.Binding = new Binding("Battles_lost");
                statDataGrid.Columns.Add(textColumn3);

                statDataGrid.ItemsSource = _fractionStats;
                statDataGrid.Columns.RemoveAt(6);
                statDataGrid.Columns.RemoveAt(5);
                statDataGrid.Columns.RemoveAt(4);
                statDataGrid.Columns.RemoveAt(3);
                statDataGrid.InvalidateVisual();
            }
            
            statDataGrid.InvalidateVisual();
        }

        private void replayButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            // Show open file dialog box
            Nullable<bool> result = openDialog.ShowDialog();

            // Process open file dialog box results
            if (result == true)
            {
                // Open document
                string filename = openDialog.FileName;

                new Battle.ReplayWindow(Serializer.DeSerializeLLFile(filename)).Show();
            }
        }

        private void refreshButton_Click(object sender, RoutedEventArgs e)
        {
            _proxy.ProxyService.GetStatistic();
        }
    }
}
